﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CinemabookingWCFApp.MovieRunTimes
{

    public class MovieRunTimeService : IMovieRunTimeService
    {
        #region
        CinemabookingDBDataContext db = new CinemabookingDBDataContext();
        #endregion

        public List<MovieRunTime> GetRunTime(int RunId)
        {
            var queryResult = (from x in db.MovieRunTimes
                               where x.id == RunId
                               select x);
            return queryResult.ToList();
        }

        public List<MovieRunTime> GetAllRunTime()
        {
            var queryResult = (from x in db.MovieRunTimes                              
                               select x);
            return queryResult.ToList();                                   
        }

        public List<MovieRunTime> GetRunTimeOnMovNameAndDate(string MovName, string MovDate)
        {
            var queryResult = (from x in db.MovieRunTimes
                               where x.movieName == MovName
                               where x.dateOfMovie == MovDate
                               select x);
            return queryResult.ToList();
        }

        public List<MovieRunTime> GetRunTimeOnDate(string MovDate)
        {
            var queryResult = (from x in db.MovieRunTimes
                               where x.dateOfMovie == MovDate
                               select x);
            return queryResult.ToList();
        }        

        public List<MovieRunTime> GetRunTimeOnNameDateAndTime(string MovName, string MovDate, string MovTime)
        {
            var queryResult = (from x in db.MovieRunTimes
                               where x.movieName == MovName
                               where x.dateOfMovie == MovDate
                               where x.startTime == MovTime
                               select x);
            return queryResult.ToList();

        }

        public void SaveRunTime(string MovName, int TheaterId, string PlayTimeId, string DateId)
        {
            MovieRunTime saveRun = new MovieRunTime { movieName = MovName, theater_id = TheaterId, startTime = PlayTimeId, dateOfMovie = DateId };
            db.MovieRunTimes.InsertOnSubmit(saveRun);
            db.SubmitChanges();
        }

        public void DeleteRunTime(int RunId)
        {
            MovieRunTime delRun = (from x in db.MovieRunTimes
                                   where x.id == RunId
                                   select x).FirstOrDefault();
            if (delRun !=null)
            {
                db.MovieRunTimes.DeleteOnSubmit(delRun);
                db.SubmitChanges();
            }
        }

        public void UpdateRunTime(int RunId, string MovName, int TheaterId, string PlayTimeId, string DateId)
        {
            MovieRunTime updRun = (from x in db.MovieRunTimes
                                   where x.id == RunId
                                   select x).Single();

            updRun.movieName = MovName;
            updRun.theater_id = TheaterId;
            updRun.startTime = PlayTimeId;
            updRun.dateOfMovie = DateId;

            db.SubmitChanges();
        }

        public List<string> GetMovieNameOnId(int MovId)
        {
            var queryResult = (from x in db.MovieInfos
                               where x.id == MovId
                               select x.name);
            
            return queryResult.ToList();
        }
    }
}
